package com.tsing.cedap.common.exceptions;

import com.tsing.cedap.common.utils.ResultUtil;
import com.tsing.cedap.common.vo.ParamVerifyExceptionVo;
import com.tsing.cedap.common.vo.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResultData<?> exceptionHandler(Exception e) {
        log.error("系统错误", e);
        return ResultUtil.error(600, "系统错误");
    }

    @ExceptionHandler(BusinessException.class)
    public ResultData<?> exceptionHandler(BusinessException e) {
        log.error("系统错误", e);
        return ResultUtil.error(e.getCode(), e.getDescription());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultData<?> exceptionHandler(MethodArgumentNotValidException e) {
        List<ParamVerifyExceptionVo> paramVerifyExceptionVos = new ArrayList<>();
        for (FieldError error : e.getBindingResult().getFieldErrors()) {
            ParamVerifyExceptionVo paramVerifyExceptionVo = new ParamVerifyExceptionVo();
            paramVerifyExceptionVo.setDefaultMessage(error.getDefaultMessage());
            paramVerifyExceptionVo.setField(error.getField());
            paramVerifyExceptionVo.setRejectedValue(error.getRejectedValue());
            paramVerifyExceptionVos.add(paramVerifyExceptionVo);
        }
        return ResultUtil.error(601, e.getBindingResult().getFieldErrors().get(0).getDefaultMessage(), paramVerifyExceptionVos);
    }

    @ExceptionHandler(BindException.class)
    public ResultData<?> exceptionHandler(BindException e) {
        List<ParamVerifyExceptionVo> paramVerifyExceptionVos = new ArrayList<>();
        for (FieldError error : e.getBindingResult().getFieldErrors()) {
            ParamVerifyExceptionVo paramVerifyExceptionVo = new ParamVerifyExceptionVo();
            paramVerifyExceptionVo.setDefaultMessage(error.getDefaultMessage());
            paramVerifyExceptionVo.setField(error.getField());
            paramVerifyExceptionVo.setRejectedValue(error.getRejectedValue());
            paramVerifyExceptionVos.add(paramVerifyExceptionVo);
        }
        return ResultUtil.error(602, e.getBindingResult().getFieldErrors().get(0).getDefaultMessage(), paramVerifyExceptionVos);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultData<?> exceptionHandler(HttpRequestMethodNotSupportedException e) {
        return ResultUtil.error(603, "请求方式错误");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResultData<?> exceptionHandler(HttpMessageNotReadableException e) {
        return ResultUtil.error(604, "请求参数解析错误");
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResultData<?> exceptionHandler(HttpMediaTypeNotSupportedException e) {
        return ResultUtil.error(605, "ContentType错误");
    }

}
